﻿using System;
using System.Collections.Generic;
using System.Data;
using viPage.Classes;
using viPage.Providers;
using visinia.SmartEngine.Cache;
using visinia.SmartEngine.Pages;
using visinia.SmartEngine.Installer;

namespace viPage.Controllers
{
    public class viPageController
    {       
        public static List<viPageInfo> GetPages()
        {
            List<viPageInfo> list = (List<viPageInfo>)DataCache.GetCache("viPages");
            if (list == null)
            {
                list = FillPages(viPageDataProvider.Provider.GetPages());
                DataCache.SetCache("viPages", list);
            }
            return list;            
        }

        public static viPageInfo GetPage(int pageid)
        {
            return GetPages().Find(p=> p.PageId == pageid);
        }

        public static viPageInfo GetPage(string pageName)
        {
            return GetPages().Find(p => string.Compare(p.Name, pageName, true) == 0);
        }

        public static int AddPage(viPageInfo page)
        {
            if (page == null)
                return 0;
            //Create Actual visinia page
            int visiniaPageId = PageController.AddPage(new PageInfo(page.Name, page.Title, PluginController.GetInstalledPlugin("viPage").PluginId));
            IDataReader dr = viPageDataProvider.Provider.AddPage(visiniaPageId, page.Name, page.Title);
            int index = 0;
            while (dr.Read())
            {
                if (dr[0] != Convert.DBNull)
                    index = (int)dr[0];
            }
            dr.Close();
            DataCache.DeleteCacheMultiple("viPages");
            return index;
        }

        public static void UpdatePage(viPageInfo page)
        {
            if (page != null)
            {
                PageInfo webpage = PageController.GetPage(page.WebPageId);
                if (webpage != null)
                {
                    // update visinia page
                    webpage.Name = page.Name;
                    webpage.Title = page.Title;
                    PageController.UpdatePage(webpage);
                    // update viPage page
                    viPageDataProvider.Provider.UpdatePage(page.PageId, page.WebPageId, page.Name, page.Title);
                    DataCache.DeleteCacheMultiple("viPages");
                }
            }            
        }

        public static void DeletePage(int pageid)
        {
            viPageInfo page = GetPage(pageid);
            if (page != null)
            {
                // remove visinia page
                PageController.DeletePage(page.WebPageId);
                viPageDataProvider.Provider.DeletePage(page.PageId);
                DataCache.DeleteCacheMultiple("viPages");
            }
        }

        #region HELPERS       
        private static List<viPageInfo> FillPages(IDataReader dr)
        {
            List<viPageInfo> list = new List<viPageInfo>();
            while (dr.Read())
            {
                list.Add(ActualRead(dr, false));
            }
            dr.Close();
            return list;
        }
        private static viPageInfo ActualRead(IDataReader dr, bool CloseDataReader)
        {
            viPageInfo page = new viPageInfo();
            if (dr["PageId"] != Convert.DBNull)
                page.PageId = (int)dr["PageId"];
            if (dr["WebPageId"] != Convert.DBNull)
                page.WebPageId = (int)dr["WebPageId"];  
            if (dr["Name"] != Convert.DBNull)
                page.Name = (string)dr["Name"];
            if (dr["Title"] != Convert.DBNull)
                page.Title = (string)dr["Title"];                    
            if (CloseDataReader)
                dr.Close();
            return page;
        }
        #endregion
    }
}
